const path = require("path");
const FileManagerPlugin = require("filemanager-webpack-plugin");

const resolve = dir => {
  return path.join(__dirname, dir);
};
const Timestamp = new Date().getTime();
const port = process.env.port || process.env.npm_config_port || 8080;
const configure = {
  lintOnSave: false, // 如果你不需要使用eslint，把lintOnSave设为false即可
  devServer: {
    port: 8080,
    open: true, //自动打开浏览器
    // https:true
  },
  configureWebpack: config => {
    //配置 去除断点和打印
    if (process.env.NODE_ENV !== "development") {
      // 取消console打印
      config.optimization.minimizer[0].options.terserOptions.compress.warnings = false;
      config.optimization.minimizer[0].options.terserOptions.compress.drop_console = true;
      config.optimization.minimizer[0].options.terserOptions.compress.drop_debugger = true;
      config.optimization.minimizer[0].options.terserOptions.compress.pure_funcs = [
        "console.log"
      ];
      //配置打包
      config.optimization.minimizer.push(
        new FileManagerPlugin({
          events: {
            onEnd: {
              delete: [
                //首先需要删除项目根目录下的dist.zip
                "./dist.zip"
              ],
              archive: [
                //然后我们选择yztyw文件夹将之打包成dist.zip并放在根目录
                { source: "./dist", destination: "./dist.zip" }
              ]
            }
          }
        })
      );
    }
    config.resolve.alias["@"] = resolve("src");
    config.name = "星图数据";
  },
  chainWebpack(config) {
    if (process.env.NODE_ENV !== "development") {
      // 清除css，js版本号
      config.output.filename(`js/[name].[chunkhash].${Timestamp}.js`).end();
      config.output.chunkFilename(`js/[id].[chunkhash].${Timestamp}.js`).end();
      // 为生产环境修改配置...
      config.plugin("extract-css").tap(args => [
        {
          filename: `css/[name].[chunkhash].${Timestamp}.css`,
          chunkFilename: `css/[name].[chunkhash].${Timestamp}.css`
        }
      ]);
    }
  }
};

//proxy
if (process.env.ENV === 'development') {
  configure.devServer.proxy = {
    "/apis": {
      target: process.env.VUE_APP_BASE_URL,
      changeOrigin: true, // 是否跨域，虚拟的站点需要更管origin
      pathRewrite: {
        '^/apis': ''
      }
    }
  }
}

module.exports = configure;